package com.jameci.luntan.controller;

import com.jameci.luntan.dto.PostDTO;
import com.jameci.luntan.mapper.PostMapper;
import com.jameci.luntan.mapper.UserMapper;
import com.jameci.luntan.model.Post;
import com.jameci.luntan.model.PostExample;
import com.jameci.luntan.model.User;
import com.jameci.luntan.service.PostService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class PublishController {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/publish")
    public String publish(
            HttpServletRequest request,
            Model model,
            @RequestParam(name = "id", defaultValue = "-1") int  id
    ) {
        Post post = null;
        if(id != -1) {
            post = postMapper.selectByPrimaryKey(id);
            User user = (User) request.getSession().getAttribute("user");
            if(user.getId() != post.getCreator()) post = null;
        }
        if(post != null) {
            model.addAttribute("id", post.getId());
            model.addAttribute("title", post.getTitle());
            model.addAttribute("discription", post.getDiscription());
            model.addAttribute("tag", post.getTag());
        }
        return "publish";
    }

    @PostMapping("/publish")
    public String doPublish(
            @RequestParam(name = "id", defaultValue = "-1") int id,
            @RequestParam("title") String title,
            @RequestParam("discription") String discription,
            @RequestParam("tag") String tag,
            HttpServletRequest request,
            Model model
    ) {
        System.out.println("publish start");
        model.addAttribute("id", id);
        model.addAttribute("title", title);
        model.addAttribute("discription", discription);
        model.addAttribute("tag", tag);
        //TODO:加入正则表达式的校验
        if(title == null || title == "") {
            model.addAttribute("error", "标题不能为空");
            return "publish";
        }
        if(discription == null || discription == "") {
            model.addAttribute("error", "帖子内容不能为空");
            return "publish";
        }
        if(tag == null || tag == "") {
            model.addAttribute("error", "标签不能为空");
            return "publish";
        }

        User user = (User) request.getSession().getAttribute("user");
        if(user == null) {
            model.addAttribute("error", "用户未登录");
            return "publish";
        }

        Post post = null;
        post = postMapper.selectByPrimaryKey(id);
        if(post == null || post.getCreator() != user.getId()) post = null;
        if(post == null) {
            post = new Post();
            post.setTitle(title);
            post.setDiscription(discription);
            post.setTag(tag);
            post.setGmtCreate(System.currentTimeMillis());
            post.setGmtModified(post.getGmtCreate());
            post.setCreator(user.getId());
            post.setLikeCount(0);
            post.setCommentCount(0);
            post.setViewCount(0);
            System.out.println(post);
            postMapper.insert(post);
            System.out.println("发布了一条新帖");
        }else {
            post.setTitle(title);
            post.setDiscription(discription);
            post.setTag(tag);
            post.setGmtModified(System.currentTimeMillis());
            PostExample postExample = new PostExample();
            postExample.createCriteria().andIdEqualTo(post.getId());
            postMapper.updateByExample(post, postExample);
        }
        return "redirect:/";
    }
}
